/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.IMAPMIDlet;
import core.ScreenEngine;
import view.additional.CellItem;
import view.additional.MenuList;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class MenuForm extends BasicForm implements MenuList.MenuListSelection
{
    private static final int OK_COMMAND_ID = 1;
    
    private static final int POSITION_ITEM_ID = 0;
    private static final int ORDER_ITEM_ID = 1;
    private static final int CONNECTION_ITEM_ID = 2;
    private static final int EXTRA_FUNC_ITEM_ID = 3;
    private static final int EFIR_ITEM_ID = 4;
    private static final int SETTINGS_ITEM_ID = 5;
    private static final int ENTER_EXIT_ITEM_ID = 6;
    private static final int BAIKAL_ITEM_ID = 7;
    
    private MenuList items;
    private CellItem extraFunctionsItem;
    
    private PositionActionsForm positionActionsForm;
    private OrderFunctionsForm orderFunctionsForm;
    private ConnectionListForm connectionListForm;
    private ExtraFunctionsListForm extraFunctionsListForm;
    private EfirSettingsForm efirSettingsForm;
    private MenuSettingsForm menuSettingsForm;
    private EnterExitForm enterExitForm;
    private BaikalConfirmationForm baikalConfirmationForm;
    
    public MenuForm()
    {
        super("menu");
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setScrollable(false);
        
        items = new MenuList();
        items.addItem(new CellItem(POSITION_ITEM_ID, "position"));
        items.addItem(new CellItem(ORDER_ITEM_ID, "order"));
        items.addItem(new CellItem(CONNECTION_ITEM_ID, "connection"));
        extraFunctionsItem = new CellItem(EXTRA_FUNC_ITEM_ID, "extra_func");
        items.addItem(extraFunctionsItem);
        items.addItem(new CellItem(EFIR_ITEM_ID, "efir"));
        items.addItem(new CellItem(SETTINGS_ITEM_ID, "settings"));
        items.addItem(new CellItem(ENTER_EXIT_ITEM_ID, "enter_exit"));
        items.addItem(new CellItem(BAIKAL_ITEM_ID, "baikal"));
        items.setSelectionListener(this);
        addComponent(items);
        
        //Команды
        addCommand(new BackCommand());
        
        Command ok = new Command("ok", OK_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                itemSelected(items.getSelectedId());
                
                evt.consume();
            }
        };
        addCommand(ok);
    }
    
    public void show()
    {
        //Устанавливаем активные\неактивные пункты
        setItemsState();
        
        super.show();
    }
    
    public void itemSelected(int id)
    {
        switch(id)
        {
            case POSITION_ITEM_ID : showPositionActionsForm(); break;
            case ORDER_ITEM_ID : showOrderFunctionsForm(); break;
            case CONNECTION_ITEM_ID : showConnectionListForm(); break; 
            case EXTRA_FUNC_ITEM_ID : showExtraFunctionsListForm(); break;
            case EFIR_ITEM_ID : showEfirSettingsForm(); break;
            case SETTINGS_ITEM_ID : showMenuSettingsForm(); break;    
            case ENTER_EXIT_ITEM_ID : showEnterExitForm(); break;
            case BAIKAL_ITEM_ID : showBaikalConfirmationForm(); break; 
//            case BAIKAL_ITEM_ID : ScreenEngine.getEngine().showMessageBox("", Connection.LOG, -1);break;//showBaikalConfirmationForm(); break; 
        }
    }
    
    private void setItemsState()
    {
        extraFunctionsItem.setEnabled(state == CONNECTED_STATE);
    }
    
    private void showPositionActionsForm()
    {
        if (positionActionsForm == null)
        {
            positionActionsForm = new PositionActionsForm();
        }
        
        ScreenEngine.getEngine().push(positionActionsForm);
    }
    
    private void showOrderFunctionsForm()
    {
        if (orderFunctionsForm == null)
        {
            orderFunctionsForm = new OrderFunctionsForm();
        }
        
        ScreenEngine.getEngine().push(orderFunctionsForm);
    }
    
    private void showConnectionListForm()
    {
        if (connectionListForm == null)
        {
            connectionListForm = new ConnectionListForm();
        }
        
        ScreenEngine.getEngine().push(connectionListForm);
    }
    
    private void showExtraFunctionsListForm()
    {
        if (extraFunctionsListForm == null)
        {
            extraFunctionsListForm = new ExtraFunctionsListForm();
        }
        
        ScreenEngine.getEngine().push(extraFunctionsListForm);
    }
    
    private void showEfirSettingsForm()
    {
        if (efirSettingsForm == null)
        {
            efirSettingsForm = new EfirSettingsForm();
        }
        
        ScreenEngine.getEngine().push(efirSettingsForm);
    }
    
    private void showMenuSettingsForm()
    {
        if (menuSettingsForm == null)
        {
            menuSettingsForm = new MenuSettingsForm();
        }
        
        ScreenEngine.getEngine().push(menuSettingsForm);
    }
    
    private void showEnterExitForm()
    {
        if (enterExitForm == null)
        {
            enterExitForm = new EnterExitForm();
        }
        
        ScreenEngine.getEngine().push(enterExitForm);
    }
    
    private void showBaikalConfirmationForm()
    {
        if (baikalConfirmationForm == null)
        {
            baikalConfirmationForm = new BaikalConfirmationForm();
        }
        
        ScreenEngine.getEngine().push(baikalConfirmationForm);
    }
}
